package com.sxz.sbs.module.sms.adapter;

import android.support.annotation.Nullable;
import android.view.View;
import android.widget.CheckBox;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.sxz.sbs.R;
import com.sxz.sbs.module.sms.mode.SmsCustomBean;
import com.willy.ratingbar.ScaleRatingBar;

import java.util.List;

/**
 * 选择短信用户适配器
 */

public class ChoseSmsClientAdapter extends BaseQuickAdapter<SmsCustomBean,ChoseSmsClientAdapter.ChoseSmsClientHolder>{

    public ChoseSmsClientAdapter(@Nullable List<SmsCustomBean> dataList) {
        super(R.layout.item_chose_sms_client_layout,dataList);
    }
    private OnItemChoseClick onItemChoseClick;

    public void setOnItemChoseClick(OnItemChoseClick onItemChoseClick) {
        this.onItemChoseClick = onItemChoseClick;
    }

    @Override
    protected void convert(final ChoseSmsClientHolder helper, final SmsCustomBean item) {
        helper.setIsRecyclable(false);
        if (item.getVal_StarLevel() <= 0) {
            helper.ratingBar.setVisibility(View.GONE);
        } else {
            helper.ratingBar.setVisibility(View.VISIBLE);
            helper.ratingBar.setNumStars(item.getVal_StarLevel());
            helper.ratingBar.setRating(item.getVal_StarLevel());
        }
        helper.setText(R.id.tv_client_name,item.getTxt_UserName());
        helper.setText(R.id.tv_client_phone,item.getTxt_Phone());
        if (item.isSelected()) {
            helper.checkBox.setChecked(true);
        } else {
            helper.checkBox.setChecked(false);
        }
        helper.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (helper.checkBox.isChecked()) {
                    helper.checkBox.setChecked(false);
                    item.setSelected(false);
                } else {
                    helper.checkBox.setChecked(true);
                    item.setSelected(true);
                }
                if (onItemChoseClick != null) {
                    onItemChoseClick.onSelectClick(helper.checkBox.isChecked(),helper.getAdapterPosition());
                }
            }
        });
        helper.checkBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!helper.checkBox.isChecked()) {
                    item.setSelected(true);
                } else {
                    item.setSelected(false);
                }
                if (onItemChoseClick != null) {
                    onItemChoseClick.onSelectClick(helper.checkBox.isChecked(),helper.getAdapterPosition());
                }
            }
        });

    }
    public class ChoseSmsClientHolder extends BaseViewHolder {
        ScaleRatingBar ratingBar;
        CheckBox checkBox;
        public ChoseSmsClientHolder(View view) {
            super(view);
            ratingBar = view.findViewById(R.id.simpleRatingBar);
            checkBox = view.findViewById(R.id.cb_sms_client);
        }
    }
    public interface OnItemChoseClick {
        void onSelectClick(boolean isSelected,int position);
    }
}
